package net.crashtoot.picasa.core.service.impl;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import net.crashtoot.picasa.core.dao.PhotoArchiveDao;
import net.crashtoot.picasa.core.dao.PhotoDao;
import net.crashtoot.picasa.core.model.Account;
import net.crashtoot.picasa.core.model.Photo;
import net.crashtoot.picasa.core.service.PhotoArchiveService;

import org.springframework.stereotype.Service;

import com.google.gdata.util.ServiceException;

@Service
public class PhotoArchiveServiceImpl implements PhotoArchiveService {
    // private static final Log log = LogFactory.getLog(PhotoArchiveServiceImpl.class);

    @Resource
    private PhotoDao photoDao;

    @Resource
    private PhotoArchiveDao archiveDao;

    @Override
    public List<File> listUnUploadPhotos(final String directory, final Account account, final String album)
            throws IOException, ServiceException {
        final List<File> localFiles = archiveDao.listPhotos(directory);

        final List<File> unUpload = new ArrayList<File>();
        if (localFiles != null) {
            // liste les photos de l'album
            final List<Photo> photos = photoDao.getPhotos(album, account);

            final Map<String, Photo> uploaded = new HashMap<String, Photo>();
            if (photos != null) {
                for (final Photo photo : photos) {
                    uploaded.put(photo.getName(), photo);
                }
            }

            for (final File localFile : localFiles) {
                if (uploaded == null || !uploaded.keySet().contains(localFile.getName())) {
                    unUpload.add(localFile);
                }
            }
        }

        return unUpload;
    }

}
